package tree;

import java.util.ArrayList;
import java.util.List;

/**
 * @author dx
 * @version 1.0
 * @date 2022/5/10 9:42
 * @description: 给定一颗二叉搜索树，找出其中第K大的节点
 */
public class Kth_Number {

}


class kth_Solution{
    public int KthLargest(TreeNode root, int k){
        List<Integer> list = new ArrayList<Integer>();
        int ans = -1;
        int count = 0;
        inoder(list,root);
        int size = list.size();
        for(int i : list){
            count++;
            if(count == size-k+1)
                ans = i;
        }
        return ans;
    }
    public void inoder(List<Integer> list ,TreeNode root){
        if(root == null){
            return;
        }
        inoder(list,root.left);
        list.add(root.val);
        inoder(list,root.right);
    }
}
